<?php
/**
 * 语音播报系统测试程序
 * 用于测试改动后的动态文字转语音功能
 */

error_reporting(E_ALL);
ini_set('display_errors', 'on');

// 颜色输出类
class ConsoleColor {
    const RESET = "\033[0m";
    const RED = "\033[31m";
    const GREEN = "\033[32m";
    const YELLOW = "\033[33m";
    const BLUE = "\033[34m";
    const CYAN = "\033[36m";
    const WHITE = "\033[37m";

    public static function success($text) {
        return self::GREEN . "✅ " . $text . self::RESET;
    }

    public static function error($text) {
        return self::RED . "❌ " . $text . self::RESET;
    }

    public static function warning($text) {
        return self::YELLOW . "⚠️ " . $text . self::RESET;
    }

    public static function info($text) {
        return self::BLUE . "ℹ️ " . $text . self::RESET;
    }

    public static function title($text) {
        return self::CYAN . "🎤 " . $text . self::RESET;
    }
}

// 测试类
class VoiceSystemTest {
    private $testResults = [];
    private $testCount = 0;
    private $passCount = 0;

    public function run() {
        echo "\n";
        echo ConsoleColor::title("语音播报系统测试程序") . "\n";
        echo str_repeat("=", 60) . "\n\n";

        // 测试 1: 检查文件是否存在
        $this->testFileExists();

        // 测试 2: 检查函数是否存在
        $this->testFunctionExists();

        // 测试 3: 测试文字生成逻辑
        $this->testVoiceMessageGeneration();

        // 测试 4: 测试 WebSocket 消息格式
        $this->testWebSocketMessageFormat();

        // 测试 5: 测试浏览器兼容性
        $this->testBrowserCompatibility();

        // 输出测试结果
        $this->printResults();
    }

    private function testFileExists() {
        echo ConsoleColor::info("测试 1: 检查文件是否存在") . "\n";
        echo str_repeat("-", 60) . "\n";

        $files = [
            'tools/device_tcp/simple_server.php' => 'TCP 服务器',
            'public/voice_player.html' => '前端播报页面',
        ];

        foreach ($files as $file => $desc) {
            $this->testCount++;
            if (file_exists($file)) {
                echo ConsoleColor::success("$desc 存在: $file") . "\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("$desc 不存在: $file") . "\n";
            }
        }
        echo "\n";
    }

    private function testFunctionExists() {
        echo ConsoleColor::info("测试 2: 检查关键函数是否存在") . "\n";
        echo str_repeat("-", 60) . "\n";

        $phpFile = 'tools/device_tcp/simple_server.php';
        if (!file_exists($phpFile)) {
            echo ConsoleColor::error("文件不存在，跳过此测试") . "\n\n";
            return;
        }

        $content = file_get_contents($phpFile);
        $functions = [
            'playVoice' => '语音播放函数',
            'pushToWebSocket' => 'WebSocket 推送函数',
            'handleSnapshotFace' => '人脸识别处理函数',
        ];

        foreach ($functions as $func => $desc) {
            $this->testCount++;
            if (strpos($content, "function $func") !== false) {
                echo ConsoleColor::success("$desc 存在: $func()") . "\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("$desc 不存在: $func()") . "\n";
            }
        }
        echo "\n";
    }

    private function testVoiceMessageGeneration() {
        echo ConsoleColor::info("测试 3: 测试文字生成逻辑") . "\n";
        echo str_repeat("-", 60) . "\n";

        $phpFile = 'tools/device_tcp/simple_server.php';
        if (!file_exists($phpFile)) {
            echo ConsoleColor::error("文件不存在，跳过此测试") . "\n\n";
            return;
        }

        $content = file_get_contents($phpFile);

        // 检查是否包含动态文字生成逻辑
        $testCases = [
            'voice_message' => '推送 voice_message 字段',
            "case 'success'" => '签到开始场景',
            "case 'end'" => '签到结束场景',
            "case 'duplicate'" => '重复刷脸场景',
            'member_name' => '会员名字支持',
            'points' => '积分支持',
        ];

        foreach ($testCases as $keyword => $desc) {
            $this->testCount++;
            if (strpos($content, $keyword) !== false) {
                echo ConsoleColor::success("$desc 已实现") . "\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("$desc 未实现") . "\n";
            }
        }
        echo "\n";
    }

    private function testWebSocketMessageFormat() {
        echo ConsoleColor::info("测试 4: 测试 WebSocket 消息格式") . "\n";
        echo str_repeat("-", 60) . "\n";

        $phpFile = 'tools/device_tcp/simple_server.php';
        if (!file_exists($phpFile)) {
            echo ConsoleColor::error("文件不存在，跳过此测试") . "\n\n";
            return;
        }

        $content = file_get_contents($phpFile);

        // 检查 WebSocket 消息格式
        $messageFields = [
            "'type' => 'voice_play'" => '消息类型字段',
            "'voice_message'" => '语音文字字段',
            "'voice_type'" => '语音类型字段',
            "'time'" => '时间戳字段',
            "'data'" => '数据字段',
        ];

        foreach ($messageFields as $field => $desc) {
            $this->testCount++;
            if (strpos($content, $field) !== false) {
                echo ConsoleColor::success("$desc 已实现: $field") . "\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::warning("$desc 未实现: $field") . "\n";
            }
        }
        echo "\n";
    }

    private function testBrowserCompatibility() {
        echo ConsoleColor::info("测试 5: 测试浏览器兼容性") . "\n";
        echo str_repeat("-", 60) . "\n";

        $htmlFile = 'public/voice_player.html';
        if (!file_exists($htmlFile)) {
            echo ConsoleColor::error("文件不存在，跳过此测试") . "\n\n";
            return;
        }

        $content = file_get_contents($htmlFile);

        // 检查 Web Speech API 支持
        $apiFeatures = [
            'SpeechSynthesisUtterance' => 'Web Speech API 支持',
            'speechSynthesis' => 'Speech Synthesis 支持',
            'webkitSpeechSynthesisUtterance' => 'Webkit 前缀支持',
            "lang = 'zh-CN'" => '中文语言支持',
            'utterance.rate' => '语速调整支持',
            'utterance.pitch' => '音调调整支持',
            'utterance.volume' => '音量调整支持',
        ];

        foreach ($apiFeatures as $feature => $desc) {
            $this->testCount++;
            if (strpos($content, $feature) !== false) {
                echo ConsoleColor::success("$desc 已实现") . "\n";
                $this->passCount++;
            } else {
                echo ConsoleColor::error("$desc 未实现") . "\n";
            }
        }
        echo "\n";
    }

    private function printResults() {
        echo str_repeat("=", 60) . "\n";
        echo ConsoleColor::title("测试结果总结") . "\n";
        echo str_repeat("=", 60) . "\n\n";

        $passPercentage = ($this->passCount / $this->testCount) * 100;

        echo "总测试数: " . ConsoleColor::CYAN . $this->testCount . ConsoleColor::RESET . "\n";
        echo "通过数: " . ConsoleColor::GREEN . $this->passCount . ConsoleColor::RESET . "\n";
        echo "失败数: " . ConsoleColor::RED . ($this->testCount - $this->passCount) . ConsoleColor::RESET . "\n";
        echo "通过率: " . ConsoleColor::CYAN . sprintf("%.1f%%", $passPercentage) . ConsoleColor::RESET . "\n\n";

        if ($passPercentage == 100) {
            echo ConsoleColor::success("所有测试通过！✨") . "\n";
        } elseif ($passPercentage >= 80) {
            echo ConsoleColor::warning("大部分测试通过，请检查失败项") . "\n";
        } else {
            echo ConsoleColor::error("测试失败率较高，请检查代码") . "\n";
        }

        echo "\n";
        echo str_repeat("=", 60) . "\n";
        echo ConsoleColor::info("下一步建议:") . "\n";
        echo str_repeat("-", 60) . "\n";
        echo "1. 启动 TCP 服务器: php tools/device_tcp/simple_server.php\n";
        echo "2. 启动 WebSocket 服务器: php websocket_server.php start\n";
        echo "3. 打开浏览器: http://localhost:8000/voice_player.html\n";
        echo "4. 点击 '🔊 测试语音' 按钮进行功能测试\n";
        echo "5. 查看浏览器控制台 (F12) 的日志输出\n";
        echo "\n";
    }
}

// 运行测试
$test = new VoiceSystemTest();
$test->run();
?>

